//
//  XMTabBarViewController.m
//  XM
//
//  Created by mingyue on 2017/12/18.
//  Copyright © 2017年 mingyue. All rights reserved.
//

#import "XMTabBarViewController.h"
#import "XMNavigationController.h"
#import "HomeViewController.h"
#import "FoundViewController.h"
#import "MeViewController.h"
@interface XMTabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation XMTabBarViewController

+ (void)initialize {
    // 设置为不透明
    [[UITabBar appearance] setTranslucent:NO];
    // 设置背景颜色
    [UITabBar appearance].barTintColor = TabBarTintColor;
    ;
    //  拿到整个导航控制器的外观
    UITabBarItem * item = [UITabBarItem appearance];
    item.titlePositionAdjustment = UIOffsetMake(0.0, -0.5);
    
    // 普通状态
    NSMutableDictionary * normalAtts = [NSMutableDictionary dictionary];
    normalAtts[NSFontAttributeName] = [UIFont systemFontOfSize:11];
    normalAtts[NSForegroundColorAttributeName] = TextColorGoldGrayTH;
    [item setTitleTextAttributes:normalAtts forState:UIControlStateNormal];
    
    // 选中状态
    NSMutableDictionary *selectAtts = [NSMutableDictionary dictionary];
    selectAtts[NSFontAttributeName] = [UIFont systemFontOfSize:11];
    selectAtts[NSForegroundColorAttributeName] = TextColorGoldTH;
    [item setTitleTextAttributes:selectAtts forState:UIControlStateSelected];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
//        [self GT_setupChildControllers];
    
    [self addTabBarChildViewController];
    
}


-(void)addTabBarChildViewController
{
    [self setTabBarChildController:[[HomeViewController alloc] init] title:@"首页" image:@"tabbar_home" selectImage:@"tabbar_home_selected"];
    [self setTabBarChildController:[[FoundViewController alloc] init] title:@"发现" image:@"tabbar_message_center" selectImage:@"tabbar_message_center_selected"];
   
    [self setTabBarChildController:[[MeViewController alloc] init] title:@"我的" image:@"tabbar_profile" selectImage:@"tabbar_profile_selected"];
}

// 添加tabbar的子viewcontroller
- (void)setTabBarChildController:(UIViewController*)controller title:(NSString*)title image:(NSString*)imageStr selectImage:(NSString*)selectImageStr{
    XMNavigationController* nav = [[XMNavigationController alloc] initWithRootViewController:controller];
    nav.tabBarItem.title = title;
    nav.tabBarItem.image = [[UIImage imageNamed:imageStr] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageStr]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self addChildViewController:nav];
}



//-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
//{
//   XMNavigationController * navigatVC = (XMNavigationController *)viewController;
//    NSInteger index =  tabBarController.selectedIndex;
//    NSArray * array = @[@"首页",@"发现",@"我的"];
//
//    UIViewController * vc =  [navigatVC.viewControllers firstObject];
//    if ([vc isKindOfClass:[MeViewController class]]) {
//        MeViewController * meVc = (MeViewController *)vc;
//        [meVc refreshMe_Data];
//    }
//    return  YES;
//}


#pragma mark - rubbish methed
////设置所有子控制器
//- (void)GT_setupChildControllers {
//    NSString * tabFile = [[NSBundle mainBundle] pathForResource:@"TabBar" ofType:@"plist"];
//    NSArray *array = [NSArray arrayWithContentsOfFile:tabFile];
//    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:1];
//    for (NSDictionary *dict in array) {
//        UIViewController *vc = [self GT_controllerWithDictionary:dict];
//        [arrayM addObject:vc];
//    }
//    self.viewControllers = [arrayM copy];
//    //    self.selectedIndex = 1;
//}


/**
 使用字典创建控制器
 
 @param dict 包含控制器信息的字典
 
 @return 控制器
 */
//- (UIViewController *)GT_controllerWithDictionary:(NSDictionary *)dict {
//    NSString *clsName   = dict[@"clsName"];
//    NSString *title     = dict[@"title"];
//    NSString *imageName = dict[@"imageName"];
//
//    Class className = NSClassFromString(clsName);
//
//    UIViewController *vc = [[className alloc]init];
//
//    vc.title = title;
//
//
//    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:@"#cda269"]} forState:UIControlStateSelected];
//    vc.tabBarItem.image = [UIImage imageWithOriginalName:[NSString stringWithFormat:@"%@%@",@"tabbar_",imageName]];
//
//    vc.tabBarItem.selectedImage = [UIImage imageWithOriginalName:[NSString stringWithFormat:@"%@%@%@",@"tabbar_",imageName,@"_selected"]];
//    vc.tabBarItem.imageInsets = UIEdgeInsetsMake(1, 0, -1, 0);
//
//    Class navi = NSClassFromString(@"XMNavigationController");
//    UINavigationController *nav = [[navi alloc]initWithRootViewController:vc];
//    return nav;
//    //    return vc;
//}
//
//-(void)dealloc
//{
//    NSLog(@"========== tarbar 调用了 dealloc 的方法");
//}


@end

